package com.stenio.fancat.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.domain.ActType;
import com.stenio.fancat.domain.Activity;
import com.stenio.fancat.domain.bo.ActivityBO;
import com.stenio.fancat.form.ActivityForm;
import com.stenio.fancat.service.ActivityService;
import com.stenio.fancat.util.ResultUtil;
import com.stenio.fancat.util.WebUtil;
import com.stenio.fancat.vo.Result;

/**
 * 活动模块controller
 */
@RestController
@RequestMapping("activity")
public class ActivityController {

	@Autowired
	private ActivityService activityService;

	/**
	 * 最热、最新、通过BrandId查询活动
	 * 
	 * @param activityForm
	 *            活动form实体类
	 * @return
	 */
	@PostMapping("query")
	@ResponseBody
	public Result queryActivity(@RequestBody ActivityForm activityForm) {
		Page<ActivityBO> page;
		try {
			Activity act = new Activity();
			BeanUtils.copyProperties(act, activityForm);
			page = activityService.queryActivityInfosPage(activityForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(page);
	}

	/**
	 * 获取活动类型
	 * 
	 * @return
	 */
	@PostMapping("queryType")
	@ResponseBody
	public Result queryActivity() {
		List<ActType> list = null;
		try {
			list = activityService.queryActivityType();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(list);
	}

	/**
	 * 发布活动
	 * 
	 * @param activityForm
	 *            活动VO实体
	 * @return
	 */
	@PostMapping(value = "/publish")
	@ResponseBody
	public Result publish(@RequestBody ActivityForm activityForm) {
		try {
			activityService.saveActivity(activityForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success();
	}

	/**
	 * 转发活动
	 * 
	 * @param activityForm
	 *            活动VO实体
	 * @return
	 */
	@PostMapping(value = "/resend")
	@ResponseBody
	public Result resendActivity(HttpServletRequest request, @RequestBody ActivityForm activityForm) {
		try {
			activityService.saveActOperation(activityForm.getActId(), activityForm.getUserId(), 1,
					WebUtil.getIpAddress(request));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success();
	}

	/**
	 * 点击活动
	 * 
	 * @param activityForm
	 *            活动VO实体
	 * @return
	 */
	@PostMapping(value = "/tap")
	@ResponseBody
	public Result tapActivity(HttpServletRequest request, @RequestBody ActivityForm activityForm) {
		try {
			activityService.saveActOperation(activityForm.getActId(), activityForm.getUserId(), 0,
					WebUtil.getIpAddress(request));
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success();
	}
}